package com.kevin.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
    private static Pattern linePattern = Pattern.compile("_(\\w)");
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * 格式化字节数 按字节数的大小格式化为Kb、Mb等实际大小
     * 
     * @Title byteFormat
     * @author 吕凯
     * @date 2013-8-19 上午11:41:19
     * @param size
     *            原字节数
     * @param dec
     *            保留位数
     * @return String
     */
    public static String byteFormat(long size, int dec) {
        String[] sizearr = { " B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB" };
        int pos = 0;
        double sized = (double) size;
        while (sized >= 1024) {
            sized /= 1024;
            pos++;
        }
        return Arith.round(sized, dec) + "" + sizearr[pos];

    }

    /**
     * 格式化字节数 按字节数的大小格式化为Kb、Mb等实际大小
     * 
     * @Title byteFormat
     * @author 吕凯
     * @date 2013-8-19 上午11:41:19
     * @param size
     * @return String
     */
    public static String byteFormat(long size) {
        return byteFormat(size, 2);

    }

    public static String timeFormat(long time) {
        if (time < 1000l) {
            return time + "毫秒";
        } else if (time < 1000 * 60l) {
            return time / 1000 + "秒" + time % 1000 + "毫秒";
        } else if (time < 1000 * 60 * 60l) {
            return time / (1000 * 60) + "分" + (time % (1000 * 60)) / 1000 + "秒";
        } else if (time < 1000 * 60 * 60 * 24) {
            return time / (1000 * 60 * 60) + "小时" + (time % (1000 * 60 * 60)) / (1000 * 60) + "分" + (time % (1000 * 60 * 60)) % (1000 * 6)
                    + "秒";
        }
        return "大于" + time / (1000 * 60 * 60) + "小时";
    }

    /**
     * 获取值，如果为null返回空字符
     * 
     * @Title getStringValue
     * @author 吕凯
     * @date 2016年8月24日 下午2:31:50
     * @return String
     */
    public static String getStringValue(String value) {
        return value == null ? "" : value;
    }

    // 首字母转小写
    public static String toLowerCaseFirst(String s) {
        if (Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    // 首字母转大写
    public static String toUpperCaseFirst(String s) {
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        if (str == null) {
            str = "";
        }
        str = str.replaceAll("__", "_");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 驼峰转下划线,效率比上面高 */
    public static String humpToLine(String str) {
        if (str == null) {
            str = "";
        }
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
